//图形验证码登录
// pages/loginImageCode/loginImageCode.js
var ubt_js = require("../../utils/ubt.js");
let common_js = require("../../utils/common.js");
var name_js = require("../../utils/ubtButtonName.js");
var base64_js = require("../../utils/base64src.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeImgUrl: '',
    submitInfo: {
      code: '',
      score: 0,
      isNextEnable: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);

    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //初始化数据
  initData: function(options) {
    //submitInfo
    let tmpBase64Data = options.imgBase64Data;
    //解析data
    base64_js.imageUrlWithBase64src(tmpBase64Data, res => {
      //update
      this.setData({
        codeImgUrl: res
      });
    });
  },

  //输入图形验证码
  bindCodeInput: function(e) {
    var tmp = e.detail.value;
    var tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.code.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.code.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.code = tmp;
    if (tmpSubmitInfo.score == 1) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //下一步
  onNext: function() {
    //调用方法，返回上一页
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];
    //data
    var tmpE = {};
    tmpE.detail = {};
    tmpE.detail.value = this.data.submitInfo.code;
    prePage.onCode(tmpE);
    //返回
    wx.navigateBack({});
  }
})